package br.com.werp.regras.geral;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.dbcp.DelegatingPreparedStatement;

/**
 * @author Sobreira Created on 25/08/2004
 * 
 * 
 */
public class Debuger extends GerenciadorErros {
	public static void debugar(String s, Class source) {
		saidaPadrao(s);
	}

	public static void debugar(Object o, Class source) {
		/**
		 * Debugar as consultas no pool do apache
		 */
		String saida = "Debug vazio";

		try {
			if (o instanceof DelegatingPreparedStatement) {
				DelegatingPreparedStatement delegatingPreparedStatement = (DelegatingPreparedStatement) o;
				Object delegate = delegatingPreparedStatement.getDelegate();

				if (delegate != null) {
					saida = delegate.toString();
				} else {
					saida = delegatingPreparedStatement.toString();
				}
			} else if (o instanceof org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement) {
				org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement delegatingPreparedStatement = (org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement) o;
				Object delegate = delegatingPreparedStatement.getDelegate();

				if (delegate != null) {
					saida = delegate.toString();
				} else {
					saida = delegatingPreparedStatement.toString();
				}
			} else {
				saida = o.toString();
			}
		} catch (Exception e) {
			e.printStackTrace();
			saida = e.getMessage();
		}

		saidaPadrao(saida + "\n" + source.getName());
	}

	public static void debugar(String string) {
		saidaPadrao(string);
	}

	public static void main(String[] args) throws Exception {
		 FileInputStream in = new FileInputStream("d:\\MaquinasForm.xml");
		 DataInputStream inn = new DataInputStream(in);
		 FileOutputStream save_xml = new FileOutputStream(new File(
		 "D:\\test2.xml"));
		 while (0 != inn.available()) {
		 save_xml.write(inn.readLine().getBytes("Cp1251"));
		 save_xml.write("\n".getBytes());
		 }

//		BufferedReader bufferedReader = new BufferedReader(
//				new InputStreamReader(new FileInputStream(
//						"d:\\MaquinasForm.xml"), "Cp1251"));
//
//		FileOutputStream save = new FileOutputStream(new File("D:\\test2.xml"));
//		BufferedWriter bufferedWriter = new BufferedWriter(
//				new OutputStreamWriter(save, "UTF-8"));
//		String line = bufferedReader.readLine();
//		while (line != null) {
//			bufferedWriter.write(line);
//			bufferedWriter.write("\n");
//			line = bufferedReader.readLine();
//		}
//
//		bufferedWriter.close();
	}
}
